package com.googlecode.garbagecan.dbtest.vertica;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.*;

// mvn install:install-file -DgroupId=com.vertica -DartifactId=vertica -Dversion=4.1.7_jdk_5 -Dpackaging=jar -Dfile=vertica_4.1.7_jdk_5.jar -DgeneratePom=true
public class TestLicense {
	private static final String driver = "com.vertica.Driver";

	// Vertica 4x
	private static final String url_4x = "jdbc:vertica://perfqa04:5550/fkong";
	private static final String username_4x = "pa8";
	private static final String password_4x = "fkong";

	// Vertica 5x
//	private static final String url_5x = "jdbc:vertica://perf2:5433/pa8_sim";
//	private static final String username_5x = "pa8";
//	private static final String password_5x  = "pa8_sim";

	// Vertica 5x
	private static final String url_5x = "jdbc:vertica://172.17.23.121:5550/fkong";
	private static final String username_5x = "dbadmin";
	private static final String password_5x  = "fkong";

	public static void main(String[] args) throws Exception {
		TestLicense ts = new TestLicense();

		Connection conn;
		conn = ts.get4XConnection();
		System.out.println("============== Vertica 4X Version==============");
		System.out.println(ts.getVersion(conn));
		System.out.println("============== Vertica 4X License==============");
		System.out.println(ts.getLicense(conn));
		conn.close();

		conn = ts.get5XConnection();
		System.out.println("============== Vertica 5X Version==============");
		System.out.println(ts.getVersion(conn));
		System.out.println("============== Vertica 5X License==============");
		System.out.println(ts.getLicense(conn));
		System.out.println("============== Vertica 5X Size==============");
		System.out.println(ts.getSize(conn));
		conn.close();
	}

	private String getVersion(Connection conn) throws Exception {
		String sql = "SELECT VERSION();";
		QueryRunner queryRunner = new QueryRunner();
		String version = queryRunner.query(conn, sql, new ResultSetHandler<String>() {
			public String handle(ResultSet rs) throws SQLException {
				if (!rs.next()) {
					return null;
				}
				String version = rs.getString(1);
				return version;
			}
		});
		version = version.replace("Vertica Analytic Database v", "");
		version = version.substring(0, 1);
		return version;
	}
	
	private String getLicense(Connection conn) throws Exception {
		String sql = "SELECT DISPLAY_LICENSE();";
		QueryRunner queryRunner = new QueryRunner();
		String license = queryRunner.query(conn, sql, new ResultSetHandler<String>() {
			public String handle(ResultSet rs) throws SQLException {
				if (!rs.next()) {
					return null;
				}
				String license = rs.getString(1);
				return license;
			}
		});
		return license;
	}

	private String getSize(Connection conn) throws Exception {
		//String sql = "SELECT GET_COMPLIANCE_STATUS();";
		//String sql = "SELECT AUDIT('');";
		String sql = "SELECT AUDIT('', 0, 100);";
		QueryRunner queryRunner = new QueryRunner();
		String size = queryRunner.query(conn, sql, new ResultSetHandler<String>() {
			public String handle(ResultSet rs) throws SQLException {
				if (!rs.next()) {
					return null;
				}
				String size = rs.getString(1);
				try {
					ByteArrayInputStream bais = new ByteArrayInputStream(size.getBytes());
					BufferedReader br = new BufferedReader(new InputStreamReader(bais));
					String line = null;
					while((line = br.readLine()) != null) {
						//System.out.println(line);
					}
					bais.close();
				} catch(IOException ex) {
					ex.printStackTrace();
				}
				return size;
			}
		});
		return size;
	}
	
	private Connection get4XConnection() throws Exception {
		Class.forName(driver);
		Connection conn = DriverManager.getConnection(url_4x, username_4x, password_4x);
		conn.setAutoCommit(false);
		return conn;
	}

	private Connection get5XConnection() throws Exception {
		Class.forName(driver);
		Connection conn = DriverManager.getConnection(url_5x, username_5x, password_5x);
		conn.setAutoCommit(false);
		return conn;
	}
}
